home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1707 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: troll.powertech.no!news
  2. From: Geir Thomassen <geirt@powertech.no>
  3. Newsgroups: comp.lang.c
  4. Subject: Recursion in C pre-processor ?
  5. Date: Tue, 16 Jan 1996 13:06:21 -0800
  6. Organization: Jotron Electronics
  7. Message-ID: <30FC134D.3A8F@powertech.no>
  8. NNTP-Posting-Host: port-220.ppp.powertech.no
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b3 (Win16; I)
  13.  
  14. Hello World !!\n
  15.  
  16. I have a function declared as
  17.  
  18.         void func(void);
  19.  
  20. In a typical application, the call to func() is repeated many times in 
  21. a row:
  22.  
  23.         func(); func(); func(); ..... ; func();
  24.  
  25. To make my code more readable I would like a macro DoFunc(n) which expands
  26. to n calls to func(). I tried to make a recursive macro, but it did not 
  27. pass cpp.
  28.  
  29. #define DoFunc(n)\              /* Don't work !! */
  30. #if n\
  31.    func();\
  32.    DoFunc(n-1);\
  33. #endif
  34.  
  35.  
  36. Why ?? Does cpp stop expanding the #if n ... #endif in the first pass 
  37. if n=0, or does it expand forever ??
  38.  
  39. Does anyone have a better (working) idea ?? The idea of recursion is 
  40. probably a wrong track! Please help !!
  41.  
  42. Why do I want to do this ?? The function func() emits a NOP assembly
  43. instruction into the instruction stream. The code is for a slow 8051 
  44. micro controller  with 1 us instruction cycle, and the NOPs are used 
  45. for timing. DoFunc(5) would give me 5 us delay, in just 5 bytes of code !!
  46.  
  47. -- 
  48. | Geir Thomassen,                 | geirt@powertech.no      |
  49. | R&D Engineer, electronics (MSc) | LA7HFA, qrv 2.45GHz :-) |
  50. |                                 |                         |
  51. | Jotron Electronics a.s.         | Phone +47 33124577      |
  52. | P.O. Box 85                     | Fax   +47 33126780      |
  53. | N-3280 Tjodalyng                | Priv  +47 33458231      | 
  54. | Norway                          | Telex 21715 TRON N      |
  55. |                                                           |
  56. |      9.81 m/s^2 - the best way to accelerate a PC !       |
  57.  
  58.  
  59. PS: Sorry if this is a repost, I don't think it made it the 
  60. first time ...
  61.